'use strict';
/**
 * Created by FreezeSoul on 2015/9/30.
 */
define([
        'angular',
        'app/modules/page/define',
        'app/modules/page/services/page'
    ],
    function (angular) {
        angular.module('ad.page')
            .controller('adPageImportController', ['$scope', '$rootScope', '$uibModalInstance', 'FileUploader', 'adPageEditService', 'item', function ($scope, $rootScope, $uibModalInstance, FileUploader, service, item) {
                $scope.item = item;

                $scope.import_charts = false;
                $scope.import_datasets = false;
                $scope.import_schemas = false;

                var fileSupports = ['data', 'zip'];

                $scope.uploader = new FileUploader();
                $scope.uploader.removeAfterUpload = true;
                $scope.uploader.url = adConfig.baseServiceUrl + 'common/resource/page/import';
                $scope.uploader.queueLimit = 1;
                $scope.uploader.headers = {
                    'Authorization': 'Bearer ' + $rootScope.currentUser.UserToken
                };

                $scope.uploader.formData = [{
                    pageId: $scope.item.page_id,
                    import_charts: $scope.import_charts,
                    import_datasets: $scope.import_datasets,
                    import_schemas: $scope.import_schemas,
                }];

                $scope.uploader.filters.push({
                    name: 'fileName',
                    fn: function (item) {
                        var fileExtension = item.name.split('.').pop();
                        return fileSupports.indexOf(fileExtension.toLowerCase()) > -1;
                    }
                });

                $scope.uploader.onAfterAddingFile = function () {
                    angular.element("#resource-upload").val(null);
                };

                $scope.uploader.onCompleteAll = function () {
                    $uibModalInstance.close();
                };

                $scope.uploader.onBeforeUploadItem = function (item) {
                    item.formData = [{
                        pageId: $scope.item.page_id,
                        import_charts: $scope.import_charts,
                        import_datasets: $scope.import_datasets,
                        import_schemas: $scope.import_schemas,
                    }];
                };


                $scope.ok = function () {
                    $scope.uploader.uploadAll();
                };

                $scope.cancel = function () {
                    $uibModalInstance.dismiss('cancel');
                };
            }]);
    });